<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>会员列表</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<style>
		.layui-input{width:130px;}
		.layui-form-label{width:auto;}
		.layui-form-item .layui-input-inline{width: 130px;}
		.layui-table-cell{height:auto !important}
		</style>
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">会员名称</label>
							<div class="layui-input-inline">
								<input type="text" name="uname" placeholder="" class="layui-input">
							</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">会员等级</label>
							<div class="layui-input-inline">
							<select name="level" lay-verify="">
								<option value="0">全部</option>
								{volist name="levels" id="vo"}
								<option value="{$vo['lid']}">{$vo['levelname']}</option>
								{/volist}
							</select>
							</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="member-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="member-table" lay-filter="member-table"></table>
			</div>
		</div>

		<form method="post" action="/{$MODULE_NAME}/member/export" id="myform" name="myform" enctype="multipart/form-data" style="display:none;">
		
		</form>

		<script type="text/html" id="member-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		    </button>

			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="exportcsv">
		        <i class="icon pear-icon pear-icon-download"></i>
		        导出
		    </button>
		</script>
		
		<script type="text/html" id="member-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>
		
		<script type="text/html" id="member-status">
 			{{#  if(d.status ==1){ }}
    			正常
  			{{#  } else { }}
    			冻结
  			{{#  } }}
		</script>
		
		<script type="text/html" id="member-createTime">
			{{layui.util.toDateString(d.regtime, 'yyyy-MM-dd')}}
		</script>

		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;
		  	
		  		let MODULE_PATH = "/{$MODULE_NAME}/member/";

				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '编号',
							field: 'uid',
							align: 'center',
							width: 100
						},
						{
							title: '会员名称',
							field: 'uname',
							align: 'center'
						},
						{
							title: '会员等级',
							field: 'level',
							align: 'center'
						},
						{
							title: '手机号码',
							field: 'mobile',
							align: 'center',
						},
						{
							title: '会员余额',
							field: 'account',
							align: 'center',

						},
						{
							title: '冻结余额',
							field: 'frozen_account',
							align: 'center',
						},
						{
							title: '积分',
							field: 'credit',
							align: 'center',
						},
						{
							title: '状态',
							field: 'status',
							align: 'center',
							templet: '#member-status'
						},
						{
							title: '注册时间',
							field: 'regtime',
							align: 'center',
							templet: '#member-createTime'
						},
						{
							title: '操作',
							toolbar: '#member-bar',
							align: 'center',
							width: 130
						}
					]
				]

				table.render({
					elem: '#member-table',
					url: MODULE_PATH+"memeberList",
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#member-toolbar',
					defaultToolbar: []
				});

				table.on('tool(member-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(member-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}else if (obj.event === 'exportcsv') {
						window.export_csv();
					}
				});

				form.on('submit(member-query)', function(data) {
					table.reload('member-table', {
						where: data.field,
						page:{curr:1}
					})
					return false;
				});
				
				window.add=function(){
					window.edit(null);
				}
				
				window.edit = function(obj) {
					var uid=0;
					var _title='添加会员';
					if(obj!=null){
						var uid=obj.data.uid;
							_title="编辑会员";
					}
					
					layer.open({
						type: 2,
						title: _title,
						shade: 0.1,
						area: ['800px', '600px'],
						content: MODULE_PATH + 'editMember/uid/'+uid+'.html'
					});
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该会员?', {
						icon: 3,
						title: '删除提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "delMember/uid/" + obj.data['uid'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.batchRemove = function(obj) {
					
					var checkIds = common.checkField(obj,'uid');
					
					if (checkIds === "") {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					
					layer.confirm('确定要删除这些会员?', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "delMember/uid/" + checkIds,
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('member-table');
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}
				
				//导出文件
				window.export_csv=function(){
					layer.msg("正在导出...", {
						icon: 1,
						time: 3000
					}, function() {
						
					});
					
					$("#myform").submit();
				}

				window.refresh = function(param) {
					table.reload('member-table');
				}
			})
		</script>
	</body>
</html>
